/**
 * 
 */
package com.risetek.tft.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.risetek.tft.context.SecurityLog;
import com.risetek.tft.entity.TManager;
import com.risetek.tft.service.ManagerService;
import com.zlinepay.framework.bean.AjaxJson;
import com.zlinepay.framework.bean.DwzModel;

/**
 * @author  
 * 
 */
@Controller
@RequestMapping("/manager")
public class ManagerAction extends BaseAction {
	
	@RequestMapping(value = "/hasAccount")
	@ResponseBody
	public AjaxJson hasAccount(String data) {
		AjaxJson j = new AjaxJson();
		if(managerService.queryForObjectByField("account", data) == null) {
			j.setSuccess(true);
		}
		return j;
	}
	
	@RequestMapping(value = "/add")
	public String add(  ModelMap model) {
			TManager dto = new TManager();
			model.addAttribute("dto", dto);
		return "manager/edit";
	}
	@RequestMapping(value = "/edit")
	public String edit(Integer id, ModelMap model) {
		if (id != null) {
			TManager dto = managerService.findById(id);
			model.addAttribute("dto", dto);
		}
		return "manager/edit";
	}

	@SecurityLog(descr = "", logOperate = "") 
	@RequestMapping(value="/list" )
	@ResponseBody
	public DwzModel listview(ModelMap model, DwzModel dataGrid ,String name ,HttpServletRequest request)
		  {//type=provider  
		    StringBuffer hql = new StringBuffer(" select new map(t.managerid as id,t.account as account ,t.TRole.code as rolecode ,t.TRole.name as rolename ,t.name as name,t.state as state ,t.lastlogintime as time) From TManager t  where t.managerid != 1 ");
		    if(StringUtils.isNotEmpty(name)){
		    	hql.append(" and t.account = '"+name+"' ");
		    }
			 if(StringUtils.isNotEmpty(dataGrid.getOrderField())){
				hql.append( "order by t."+dataGrid.getOrderField()+"  "+dataGrid.getOrderDirection());
			}else{
				hql.append("order by  t.id ");
			} 
	        dataGrid = this.managerService.pagedQuery(hql.toString(),"select count(*) from TManager t  where t.managerid != 1    ",   dataGrid);
	       
           return dataGrid;
	}
	
	 

	@RequestMapping(value = "/save")
	@ResponseBody
	public AjaxJson save(@ModelAttribute(value = "dto") TManager dto, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		AjaxJson json = new AjaxJson();
			if(dto.getManagerid() == null) {
				dto.setPassword(DigestUtils.md5Hex(dto.getPassword()));
				try {
					managerService.save(dto);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				TManager persistent = managerService.findById(dto.getManagerid());
				if(persistent != null) {
					if(StringUtils.isNotEmpty(dto.getPassword())) {
						persistent.setPassword(DigestUtils.md5Hex(dto.getPassword()));
					}
					persistent.setName(dto.getName());
					persistent.setTRole(dto.getTRole());
					persistent.setState(dto.getState());
					managerService.update(persistent);
				}
			}
			json.setSuccess(true);
			json.setMsg("保存成功！");
		return json;
	}

	@RequestMapping(value = "/remove")
	@ResponseBody
	public AjaxJson del(Integer[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			managerService.delete(ids);
			managerService.delete(ids);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setMsg("删除失败！");
		}
		return ajaxJson;
	}

	@Autowired
	private ManagerService managerService;

}
